package com.songshuai.customer.client.controller;

import com.songshuai.customer.client.service.CustMeetingService;
import com.songshuai.webapi.baseapi.dto.feign.request.custminiprogram.*;
import com.songshuai.webapi.baseapi.dto.feign.response.DictionaryDto;
import com.songshuai.webapi.baseapi.dto.feign.response.custminiprogram.*;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @Author:wbl
 * @Date: 2019/3/20
 */
@Slf4j
@Api("活动中心")
@RestController
@RequestMapping("/api/cust/meeting/")
public class CustMeetingController extends BaseController{

    @Autowired
    private CustMeetingService meetingService;



    @ApiOperation(value = "根据客户Id获取会议列表")
    @GetMapping("getMeetingListByUserId")
    public CompletableFuture<QueryResponse<List<MeetingListResDto>>> getMeetingListByUserId(){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getMeetingListByUserId(customId);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getMeetingListByUserId", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "根据客户Id和会议编号获取会议详情")
    @PostMapping("getMeetingDetail")
    public CompletableFuture<QueryResponse<MeetingDetailResDto>> getMeetingDetail(@RequestBody MeetingDetailReqDto parm){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getMeetingDetail(parm);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getMeetingDetail", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "根据会议编号获取邀约模板")
    @GetMapping("getInvitationTemplateByCode")
    public CompletableFuture<QueryResponse<List<InvitationTemplateResDto>>> getInvitationTemplateByCode(@RequestParam("meetingCode") String meetingCode){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getInvitationTemplateByCode(meetingCode);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getInvitationTemplateByCode", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "根据线索id和会议编号获取签到码")
    @PostMapping("getWxQRCode")
    public CompletableFuture<QueryResponse<WxQRCodeResDto>> getWxQRCode(@RequestBody WxQRCodeReqDto parm){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getWxQRCode(parm);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getWxQRCode", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    /*@ApiOperation(value = "校验签到码")
    @GetMapping("registerSign")
    public CompletableFuture<QueryResponse<Boolean>> registerSign(@RequestParam("clueguid") Long clueguid,
                                                                  @RequestParam("meetingno") String meetingno,
                                                                  @RequestParam("timestamp") String timestamp,
                                                                  @RequestParam("hashcode") String hashcode
                                                                  ){
        String openId = getCurrentCustomerOpenid();
        String customId = getCurrentcustomerId();
        if(StringUtils.isBlank(openId) || StringUtils.isBlank(customId)){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAIL);
        }
        return CompletableFuture.supplyAsync(() ->{
            Boolean result = meetingService.registerSign(clueguid, meetingno, timestamp, hashcode);
            return QueryResponse.setResData(result);
        });
    }*/

    /*@ApiOperation(value = "新增签到码签到")
    @PostMapping("QRCodeSign")
    public CompletableFuture<QueryResponse<Boolean>> QRCodeSign(@RequestBody QRCodeSignReqDto parm){
        String openId = getCurrentCustomerOpenid();
        String customId = getCurrentcustomerId();
        if(StringUtils.isBlank(openId) || StringUtils.isBlank(customId)){
            throw new BusinessException(ResultStatus.NO_LOGIN.getErrorMsg(), ResultStatus.FAIL);
        }
        return CompletableFuture.supplyAsync(() ->{
            Boolean result = meetingService.QRCodeSign(parm);
            return QueryResponse.setResData(result);
        });
    }*/

    @ApiOperation(value = "根据邀约编号获取回执单")
    @PostMapping("getMeetingReceipt")
    public CompletableFuture<QueryResponse<MeetingReceiptResDto>> getMeetingReceipt(@RequestBody MeetingReceiptReqDto parm){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getMeetingReceipt(parm);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getMeetingReceipt", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "新增回执单")
    @PostMapping("addMeetingReceipt")
    public CompletableFuture<QueryResponse<AddMeetingReceiptResDto>> addMeetingReceipt(@RequestBody AddMeetingReceiptReqDto parm){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    log.info("入住人姓名:{},备注：{}",parm.getTenants(), parm.getBack());
                    return meetingService.addMeetingReceipt(parm);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/addMeetingReceipt", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "根据字典类型获取字典信息")
    @GetMapping("getDictionaryByTypeId")
    public CompletableFuture<QueryResponse<List<DictionaryDto>>> getDictionaryByTypeId(@RequestParam("dicType") Integer dicType){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getDictionaryByTypeId(dicType);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getDictionaryByTypeId", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "根据会议编号获取车站信息")
    @GetMapping("getAreaStations")
    public CompletableFuture<QueryResponse<List<StationResDto>>> getAreaStations(@RequestParam("meetingCode") String meetingCode){
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.getAreaStations(meetingCode);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/getAreaStations", e);
                return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("uploadPicture")
    public CompletableFuture<QueryResponse> uploadPicture(@RequestParam("file") MultipartFile file) {
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.uploadPicture(file);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/uploadPicture", e);
                return new QueryResponse(ResultStatus.ERROR_FILE_UPLOAD.getErrorCode(),e.getMessage());
            }
        });
    }

    @ApiOperation(value = "图片上传")
    @PostMapping("uploadPictureOss")
    public CompletableFuture<QueryResponse> uploadPictureOss(@RequestParam("file") MultipartFile file) {
        String customId = getCurrentcustomerId();
        return CompletableFuture.supplyAsync(() ->{
            try {
                if (StringUtils.isNotBlank(customId)){
                    return meetingService.uploadPictureOss(file);
                } else {
                    return new QueryResponse<>(ResultStatus.FAIL);
                }
            } catch (Exception e) {
                log.error("/api/cust/meeting/uploadPictureOss", e);
                return new QueryResponse(ResultStatus.ERROR_FILE_UPLOAD.getErrorCode(),e.getMessage());
            }
        });
    }
}
